﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


namespace xFeedApp
{
    public partial class frmConfig : Form
    {
        public frmConfig()
        {
            InitializeComponent();
        }

        DAL.DBConnection dbCon = new xFeedApp.DAL.DBConnection();

        private void frmConfig_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        void LoadData()
        {
            dbCon.BindDataGrid(ref gvConfigTable, "tblConfig");
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            bool Active = true;
            foreach (System.Windows.Forms.Control ctl in gbConfig.Controls)
            {
                if (ctl.Name.IndexOf("txt") == 0 && ctl.Name != txtID.Name && ctl.Text == "")
                {
                    //MessageBox.Show("Please fill the form carefully!");
                    if (DialogResult.No == MessageBox.Show("Are you sure to leaves these fields blank?", "Confirm", MessageBoxButtons.YesNo))
                    {
                        return;
                    }
                    else
                        Active = false;
                }
            }
            
            DataTable tbl = (DataTable)gvConfigTable.DataSource;
            tbl.PrimaryKey = new DataColumn[] { tbl.Columns["ConfigID"]};
            tbl.Columns["ConfigID"].AutoIncrement = true;
            DataRow dr;

            if (txtID.Text == "")
            {
                dr = tbl.NewRow();
            }
            else
                dr = tbl.Rows.Find(txtID.Text);
                //gvConfigTable.Rows[gvConfigTable.Rows.Count - 1].Selected=true;;

            //DataGridViewRow dr = gvConfigTable.SelectedRows[0];
            dr["ConfigWebsite"] = txtWebsite.Text;
            dr["ConfigLinkEx"] = txtLinkRegEx.Text;
            dr["ConfigDataEx"] = txtDataRegEx.Text;
            dr["ConfigTimeToFeed"] = txtTimeToFeed.Text;
            dr["ConfigActive"] = Active;
            dr["ConfigCategory"] = txtCateToFeed.Text;
            //gvConfigTable.EndEdit(DataGridViewDataErrorContexts.Commit);
            //gvConfigTable.SelectedRows[0]

            if (txtID.Text == "")
                tbl.Rows.Add(dr);
            
            dbCon.SaveData(tbl);

            tbl.AcceptChanges();

            MessageBox.Show("Data saved successfully! To reload the table, please click refresh!","Success");
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            txtID.Text = "";
            txtWebsite.Focus();
            MessageBox.Show("Please input data in above boxes then click 'Save'");
        }

        private void gvConfigTable_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewRow dr = gvConfigTable.SelectedRows[0];
            txtWebsite.Text = dr.Cells["ConfigWebsite"].Value.ToString();
            txtLinkRegEx.Text = dr.Cells["ConfigLinkEx"].Value.ToString();
            txtDataRegEx.Text = dr.Cells["ConfigDataEx"].Value.ToString();
            txtTimeToFeed.Text = dr.Cells["ConfigTimeToFeed"].Value.ToString();
            txtID.Text = dr.Cells["ConfigID"].Value.ToString();
            txtCateToFeed.Text = dr.Cells["ConfigCategory"].Value.ToString();
            //Active = dr.Cells["ConfigActive"].Value;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (txtID.Text == "")
            {
                MessageBox.Show("Please choose a record to delete from below table!", "Action required!");
                return;
            }

            if (MessageBox.Show("Are you sure to delete this record?", "Confirm required!", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                DataTable tbl = (DataTable)gvConfigTable.DataSource;
                tbl.PrimaryKey = new DataColumn[] { tbl.Columns["ConfigID"] };

                DataRow dr;
                dr = tbl.Rows.Find(txtID.Text);

                tbl.Rows.Remove(dr);

                dbCon.SaveData(tbl);

                tbl.AcceptChanges();

                MessageBox.Show("Record deleted!", "Deleted");
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
